print("Content-Type: application/octet-stream\r\n\r\n");
{
	package ChopSet;
	$jobID = $ENV{QUERY_STRING};
	if($jobID eq ''){
		print("jobID not set.");
		exit(0);
	}
	sub sigUser1{
		&main::endCall();
		print("failed.");
		exit(0);
	}
	sub GetCPUNum{
		my($num) = 0;
		if(open(STAT, "</proc/stat")){
			while(<STAT>){
				$num = $1 if(m/^cpu(\d+)/);
			}
			close(STAT);
		}
		return $num + 1;
	}
	sub taskSet{
		my($nodeNo) = $jobID;
		$nodeNo =~ m/[^\d](\d+)$/;
		my($cpuNo) = ($1 % &GetCPUNum()) + 1;
		my($mask) = sprintf("0x%08x", 1 << $cpuNo);
		`taskset -p $mask $$`;
	}
	sub endCall{
		unlink("/tmp/ChopSet_". $ChopSet::jobID);
	}
}
$SIG{'USR1'} = "ChopSet::sigUser1";

unless(open(PIDFILE, ">/tmp/ChopSet_". $ChopSet::jobID)){
	print("PIDFILE create error.");
	exit(0);
}
print PIDFILE "$$\n";
close(PIDFILE);
#&ChopSet::taskSet();

sub endCall{
	unlink("/tmp/ChopSet_". $ChopSet::jobID);
}

END {
	&ChopSet::endCall();
}

1;
